package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.DailyFeedBackForEngineeringInstallAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/23.
 */

public class EngineeringInstallFeedBackForWaitStateActivity extends Activity implements View.OnClickListener, IEngineeringInstallOrderDetailView, ItemClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_notice;
    private TextView tv_project_name, tv_engineering_name;
    private TextView btn_engineering_info;

    private ListView lv;
    //    private View ll_engineering_info_content;
    private OrderParentBean orderParentBean;
    private CommonEnum.FeedBackState feedBackState;
    private final String TAG = EngineeringInstallFeedBackForWaitStateActivity.class.getSimpleName();
    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private List<EngineeringDailyFeedBackBean> engineeringDailyFeedBackBeanList;
    DailyFeedBackForEngineeringInstallAdapter adapter;
    List<EngineeringDailyFeedBackBean> dataList = new ArrayList<>();
    private String tag = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineeringfeedback_for_wait_state);

        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);

        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
            tag = mIntent.getStringExtra(ConstantUtil.KEY_2);
        }

        if (null != orderParentBean) {
            tv_project_name.setText(orderParentBean.getProjectName());
            tv_engineering_name.setText("/" + orderParentBean.getEngineeringName());

            feedBackState = orderParentBean.getFeedBackState();
        }

        getEgnineeringOrderDetail();

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_notice = findViewById(R.id.tv_notice);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_engineering_name = findViewById(R.id.tv_engineering_name);
        btn_engineering_info = findViewById(R.id.btn_engineering_info);
        lv = findViewById(R.id.lv);


        tv_title.setText("工程反馈");

        rl_back.setOnClickListener(this);
        btn_engineering_info.setOnClickListener(this);


        adapter = new DailyFeedBackForEngineeringInstallAdapter(this, TAG);
        adapter.setData(dataList);
        adapter.setItemClick(this);
        lv.setAdapter(adapter);
    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            engineeringDailyFeedBackBeanList = engineeringInstallationOrderBean.getEngineeringDailyFeedbackList();
            if (null != engineeringDailyFeedBackBeanList) {

                if (engineeringDailyFeedBackBeanList.size() == 0) {
                    tv_notice.setVisibility(View.VISIBLE);
                    lv.setVisibility(View.GONE);
                    if (CommonEnum.FeedBackState.WORKING == feedBackState) {
                        tv_notice.setText("工程安装中");
                    } else if (CommonEnum.FeedBackState.NO_FEEDBACK == feedBackState) {
                        tv_notice.setText("当天未反馈");
                    } else if (CommonEnum.FeedBackState.OUT_OF_PLAN == feedBackState) {
                        tv_notice.setText("工程已超出计划");
                    } else if (CommonEnum.FeedBackState.PAUSE == feedBackState) {
                        tv_notice.setText("此项工程暂停安装/" + DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getLastPauseDate()));
                    } else if (CommonEnum.FeedBackState.CREATE == feedBackState) {
                        tv_notice.setText("此项工程未开始");
                    }

                } else {
                    tv_notice.setVisibility(View.GONE);
                    dataList.clear();
                    dataList.addAll(engineeringDailyFeedBackBeanList);
                    adapter.setData(dataList);
                    adapter.notifyDataSetChanged();
                }


            }

        }
    }


    private void getEgnineeringOrderDetail() {
        engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_engineering_info:
                mIntent = new Intent(EngineeringInstallFeedBackForWaitStateActivity.this, ViewEngineeringInstallOrderDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, orderParentBean);
                startActivity(mIntent);
                break;


        }
    }


    @Override
    public void itemClick(View view, int position) {
        EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) view.getTag();

        Intent mIntent;

        CommonEnum.FeedBackType type = engineeringDailyFeedBackBean.getType();
        if (null != type) {
            if (type == CommonEnum.FeedBackType.NORMAL) {
                mIntent = new Intent(this, FeedBackSureForManagerActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
                mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                if (!TextUtils.isEmpty(tag) && tag.equals("MapForSuperFragment"))
                    mIntent.putExtra("role", "super");
                else
                    mIntent.putExtra("role", "");

                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

//                mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            } else if (type == CommonEnum.FeedBackType.UNUSUAL) {
                CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
                if (null != unusualStatus) {
                    if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {

                        mIntent = new Intent(this, FeedBackSureForManagerActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
                        mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                        if (!TextUtils.isEmpty(tag) && tag.equals("MapForSuperFragment"))
                            mIntent.putExtra("role", "super");
                        else
                            mIntent.putExtra("role", "");

                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

                    } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {

                        mIntent = new Intent(this, FeedBackSureForManagerActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
                        mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                        if (!TextUtils.isEmpty(tag) && tag.equals("MapForSuperFragment"))
                            mIntent.putExtra("role", "super");
                        else
                            mIntent.putExtra("role", "");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

//                        mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
//                        mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
//                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

                    } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                        mIntent = new Intent(this, FeedBackReplyForMnagerActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
                        mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                        if (!TextUtils.isEmpty(tag) && tag.equals("MapForSuperFragment"))
                            mIntent.putExtra("role", "super");
                        else
                            mIntent.putExtra("role", "");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

                    } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                        mIntent = new Intent(this, FeedBackReplyForMnagerActivity.class);
                        mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                        mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean.getId());
                        mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                        if (!TextUtils.isEmpty(tag) && tag.equals("MapForSuperFragment"))
                            mIntent.putExtra("role", "super");
                        else
                            mIntent.putExtra("role", "");
                        startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                    }
                }

            } else if (type == CommonEnum.FeedBackType.COMPLETED) {
                mIntent = new Intent(this, EngineeringInstallCompletionFeedBackMapForManagerActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean);
                mIntent.putExtra("feedbackbean", engineeringDailyFeedBackBean);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);


//                mIntent = new Intent(this, EngineeringInstallCompletionFeedBackMapForManagerActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, engineeringDailyFeedBackBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, engineeringInstallationOrderBean);
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        }


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getEgnineeringOrderDetail();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getItemId() {
        return null;
    }

    @Override
    public long getFeedbackDate() {
        if (!TextUtils.isEmpty(tag)) {
            if (tag.equals("MapForSuperFragment") || tag.equals("MapForManagerFragment")) {
                return System.currentTimeMillis();
            }
        }

        return 0;
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getEngineeringInstallOrderDetailSuccessed(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        initData();
    }

    @Override
    public void getEngineeringInstallOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


}
